/* js/login.js */
document.addEventListener('DOMContentLoaded', function() {
    const loginBtn = document.querySelector('.login-btn');
    const errorSpan = document.querySelector('.error');
  
    if (loginBtn) {
      loginBtn.addEventListener('click', function() {
        const username = document.querySelector('.username').value.trim();
        const password = document.querySelector('.password').value.trim();
  
        if (!username || !password) {
          alert('用户名或密码不能为空');
          return;
        }
  
        axios.post('http://localhost:8888/users/login', { username, password })
          .then(response => {
            if (response.data.code === 1) {
              alert('登录成功');
              localStorage.setItem('token', response.data.token);
              localStorage.setItem('user_id', response.data.user.id);
              window.location.href = './main.html';
            } else {
              errorSpan.textContent = response.data.message;
              errorSpan.style.display = 'block';
            }
          })
          .catch(err => {
            console.error('登录出错:', err);
            errorSpan.textContent = '登录失败，请稍后再试';
            errorSpan.style.display = 'block';
          });
      });
    }
  });
  